#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <config.h>
#include <photo_manager.h>

static PT_PhotoOpr g_ptPhotoHead;

int registerPhotoOpr(PT_PhotoOpr g_ptPhotoOpr)
{
	PT_PhotoOpr temp = g_ptPhotoHead;

	if(g_ptPhotoOpr ==NULL)
	{
		fprintf(stderr, "parama error\n");
		return -1;
	}

	if(g_ptPhotoHead ==NULL)
	{
		g_ptPhotoHead = g_ptPhotoOpr;
	}
	else
	{
		while(temp->next!=NULL)
		{
			temp = temp->next;
		}
		temp->next = g_ptPhotoOpr;
	}

	return 0;
}

void showPhotoOpr(void)
{
	int i=0;
	PT_PhotoOpr temp = g_ptPhotoHead;
	
	while(temp!=NULL)
	{
		fprintf(stdout,"%02d %s\n",i++,temp->name);
		temp = temp->next;
	}
}

int initPhotoOpr(void)
{
	int ret;

	ret = bmpInit();
	if(ret<0)
	{
		fprintf(stderr, "bmpInit error\n");
		return -1;
	}

	ret = jpegInit();
	if(ret<0)
	{
		fprintf(stderr, "jpegInit error\n");
		return -1;
	}

	ret = pngInit();
	if(ret<0)
	{
		fprintf(stderr, "pngInit error\n");
		return -1;
	}

	return 0;
}

int getPhotoData(char *filename, PT_imageInfo image)
{
	int ret;
	PT_PhotoOpr temp = g_ptPhotoHead;

	while(temp!=NULL)
	{
		ret = temp->isSupport(filename);
		if(ret ==1)
			break;

		temp = temp->next;
	}

	if(temp ==NULL)
		return -1;

	ret = temp->getDispData(filename, image);
	if(ret <0)
		return ret;

	return 0;
}

int showImage(int x, int y, PT_imageInfo imageData)
{
	int i = 0;
	int iX,iY;
	unsigned int dwColor;

	for(iY = y; iY<y+imageData->height; iY++)
	{
		for(iX=x; iX<x+imageData->width; iX++)
		{
			if(iX>=LCDWIDTH || iY >=LCDHEIGHT)
				return -1;
			if(imageData->isBMP ==0)
				dwColor = (imageData->imageDataBuff[i]<<16)+(imageData->imageDataBuff[i+1]<<8)+(imageData->imageDataBuff[i+2]<<0);
			else
				dwColor = (imageData->imageDataBuff[i]<<0)+(imageData->imageDataBuff[i+1]<<8)+(imageData->imageDataBuff[i+2]<<16);
			i +=3;
			g_ptDisplayDevice->displayShowPixel(iX, iY, dwColor);	
		}
	}

	free(imageData->imageDataBuff);
	return 0;
}


